﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
    public class IPMateriaPrima : Intermediario
    {
        public override void save<T>(T entity)
        {
            try 
	        {
                MateriaPrima mp = entity as MateriaPrima;
                dao.MateriaPrimas.InsertOnSubmit(mp);		
	        }
	        catch (Exception)
	        {		
		        throw;
	        }
        }

        public override List<T> getAll<T>()
        {
            return (from mp in dao.MateriaPrimas where mp.Activo == true select mp).ToList() as List<T>;
        }

        public override List<T> getByCriterio<T>(string att, string op, string val)
        {
            return (from m in dao.MateriaPrimas where m.IdMP == Convert.ToInt16(val) && m.Activo == true select m).ToList() as List<T>;
            //return (from est in dao.MateriaPrimas where est.IdMP == int.Parse(val) && est.Activo == true select est).ToList() as List<T>;
        }

        public override IList<T> getCriterioById<T>(string att, string op, int val)
        {
            return (from m in dao.MateriaPrimas where m.IdMedida == Convert.ToInt16(val) && m.Activo == true select m).ToList() as List<T>;
        }

        public override object getPorId(int id)
        {
            return (from m in dao.MateriaPrimas where m.IdMP == id && m.Activo == true select m);
        }

        public MateriaPrima GetMP(int id)
        {
            MateriaPrima mp = (from m in dao.MateriaPrimas where m.IdMP == id && m.Activo == true select m).First();
            return mp;
        }

        public override IList<T> getPorCriterio<T>(string name)
        {
            return (from mp in dao.MateriaPrimas where mp.nombre == name select mp).ToList() as List<T>;
            //throw new NotImplementedException();
        }

        public override void Update<T>(T entity)
        {
            try
            {

            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public override void Delete(int id)
        {
            try
            {
                var mp = (from m in dao.MateriaPrimas where m.IdMP == id select m).SingleOrDefault();

                if (mp != null)
                {
                    mp.Activo = false;
                    //dao.MateriaPrimas.DeleteOnSubmit(ct);
                    dao.SubmitChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public MateriaPrima getPorIdMP(int idMP)
        {
            MateriaPrima _entity = (MateriaPrima)(from m in dao.MateriaPrimas where m.IdMP == idMP && m.Activo == true select m);
            return _entity;
        }
    }
}
